const chalk = require("chalk");
// arg1, arg2, ..., argn 为函数的参数列表， functionBody 为函数体的字符串表示
// new Function ([arg1[, arg2[, ...argn]],] functionBody)
module.exports = function evaluate(exp, data) {
  const fn = new Function("data", `with (data) { return  ${exp} }`);
  try {
    return fn(data);
  } catch (e) {
    console.error(chalk.red("Error when evaluating filter condition: " + exp));
  }
};
